package models

import "gorm.io/gorm"

type BaseModel struct {
	gorm.Model
	UserId int64  `json:"user_id" gorm:"index:user_id;"`
	Status int    `json:"status" gorm:"type:int(1)"` // 0:正常 1:禁用
	Ext    string `json:"ext" gorm:"type:varchar(255)"`
}

func (u BaseModel) UpdateInfo(db *gorm.DB, updateItem *BaseModel) {
	if updateItem.ID == 0 {
		db.Create(updateItem)
	} else {
		db.Save(updateItem)
	}
}

func (u BaseModel) GetList(db *gorm.DB, page int, pageSize int, taskId uint) (tasks []BaseModel, rowsCount int64, err error) {
	db = db.Model(BaseModel{}).Where("task_id = ?", taskId)
	db.Count(&rowsCount)
	err = db.Offset((page - 1) * pageSize).Limit(pageSize).Find(&tasks).Error

	return tasks, rowsCount, err
}
